cmake_minimum_required(VERSION 3.15)

project(dev_dlt698 CXX)

include(FetchContent)

FetchContent_Declare(
    json
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.11.3
)

FetchContent_Declare(
    spdlog
    GIT_REPOSITORY https://github.com/gabime/spdlog.git
    GIT_TAG v1.15.0
)

FetchContent_Declare(
    scheduler
    GIT_REPOSITORY https://github.com/mr-j0nes/Scheduler.git
    GIT_TAG v0.2.1
)

# FetchContent_Declare(
#     stduuid
#     URL https://github.com/mariusbancila/stduuid/archive/refs/tags/v1.2.3.zip
# )

FetchContent_MakeAvailable(json spdlog scheduler)

include(ExternalProject)
ExternalProject_Add(
    mosq
    GIT_REPOSITORY https://github.com/eclipse-mosquitto/mosquitto.git
    GIT_TAG v2.0.20
    PREFIX ${CMAKE_CURRENT_LIST_DIR}/build/_deps/mosquitto
    CMAKE_ARGS
    -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
    -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
    -DWITH_TLS=OFF
    -DWITH_CJSON=OFF
    -DWITH_STATIC_LIBRARIES=ON
    -DWITH_THREADING=ON
    -DWITH_CLIENTS=OFF
    -DDOCUMENTATION=OFF
    -DLIBPTHREAD=pthread
    -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_LIST_DIR}/build/_deps/mosquitto-build
)

aux_source_directory(. SRC)
add_executable(${PROJECT_NAME})


target_compile_definitions(${PROJECT_NAME} PRIVATE LOG_FILE_NAME="/apps/data/log/dev_dlt698/dev_dlt698.log")
add_dependencies(${PROJECT_NAME} mosq)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/build/_deps/mosquitto-build/include)
target_link_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/build/_deps/mosquitto-build/lib)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/build/_deps/scheduler-src)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/build/_deps/scheduler-src/externals/CTPL)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/build/_deps/scheduler-src/externals/croncpp/include)

target_sources(${PROJECT_NAME} PUBLIC ${SRC})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/byteStream)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/698Frame)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/uart)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/point)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pointTable)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/deviceManager)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/device)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/mqttClient)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/mqttServer)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/mainProcess)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/threadPool)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/msgObj)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/uuid)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/ruleManager)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pollDeviceServer)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/logger)

target_link_libraries(${PROJECT_NAME} PRIVATE pthread nlohmann_json::nlohmann_json spdlog::spdlog mosquitto_static)

set_target_properties(${PROJECT_NAME} PROPERTIES 
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
)

# add_custom_command(TARGET ${PROJECT_NAME} 
#     POST_BUILD 
#     COMMAND sshpass -p root scp $<TARGET_FILE:${PROJECT_NAME}> root@192.168.10.132:/apps/dev_dlt698
# )

# add_custom_command(TARGET ${PROJECT_NAME}
#     POST_BUILD 
#     COMMAND sshpass -p root scp ${CMAKE_CURRENT_LIST_DIR}/test.json root@192.168.10.132:/apps/dev_dlt698
# )


